﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Zgq.CommonTools
{
    public static class LogHelper
    {
        #region 属性
        /// <summary>
        /// 日志目录
        /// </summary>
        private static string LogDic = string.Format("{0}\\log\\", AppDomain.CurrentDomain.BaseDirectory);

        /// <summary>
        /// 日志文件
        /// </summary>
        static string logPath
        {
            get
            {
                return string.Format("{0}\\{1}_info.log",
                        LogDic, DateTime.Now.ToString("yyyy-MM-dd"));
            }
        }
        /// <summary>
        /// 错误日志文件
        /// </summary>
        static string errLogPath
        {
            get
            {
                return string.Format("{0}\\{1}_error.log",
                        LogDic, DateTime.Now.ToString("yyyy-MM-dd"));
            }
        }
        /// <summary>
        /// 警告日志文件
        /// </summary>
        static string warnLogPath
        {
            get
            {
                return string.Format("{0}\\{1}_warn.log",
                        LogDic, DateTime.Now.ToString("yyyy-MM-dd"));
            }
        }
        #endregion

        static object lockobj = new object();
        /// <summary>
        /// 输出日志
        /// </summary>
        /// <param name="msgs"></param>
        public static void Error(params string[] msgs)
        {
            StringBuilder msgStr = new StringBuilder();
            foreach (var v in msgs)
                msgStr.Append(v);

            WriteLog(errLogPath, msgStr.ToString());
        }

        public static void Error(Exception ex)
        {
            string msg = string.Format("错误信息：{0}\n详情：\n{1}", ex.Message, ex.StackTrace);

            Error(msg);
        }
        /// <summary>
        /// 输出日志
        /// </summary>
        /// <param name="msgs"></param>
        public static void Info(params string[] msgs)
        {
            StringBuilder msgStr = new StringBuilder();
            foreach (var v in msgs)
                msgStr.Append(v);
            WriteLog(logPath, msgStr.ToString());
        }


        private static void WriteLog(string filePath, string msg)
        {
            try
            {
                //throw new Exception(filePath);
                if (!System.IO.Directory.Exists(LogDic))
                    System.IO.Directory.CreateDirectory(LogDic);

                StreamWriter sw = new StreamWriter(filePath, true, Encoding.UTF8);
                lock (lockobj)
                {
                    sw.Write("记录时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    sw.WriteLine();
                    sw.Write(msg);
                    sw.WriteLine();
                    sw.WriteLine();
                }
                sw.Close();
                sw.Dispose();
            }
            catch { }
        }

        public static void Warn(Type t, string msg)
        {
            //log4net.ILog log = log4net.LogManager.GetLogger(t);
            //log.Warn(msg);

            WriteLog(warnLogPath, msg);
        }


        /// <summary>
        /// 删除多久前的日志
        /// </summary>
        /// <param name="value"> 由整数和小数部分组成的天数。</param>
        /// <returns>删除的数量</returns>
        public static int DeleteLogFile(int value)
        {
            int delNum = 0;
            if (System.IO.Directory.Exists(LogDic)) //判断日志目录是否存在
            {
                string[] dis = Directory.GetFiles(LogDic);

                foreach (var v in dis)
                {
                    FileInfo fi = new FileInfo(v);
                    if (fi.CreationTime.AddDays(value) < DateTime.Now)//判断目录是否过期
                    {
                        fi.Delete();
                        delNum++;
                    }
                }
            }
            return delNum;
        }
    }
}

